package cn.net.vote.data.filter;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.util.Enumeration;
import java.util.HashMap;  
import java.util.Iterator;  
import java.util.Map;  
import java.util.Set;  

import javax.servlet.ReadListener;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;  
import javax.servlet.http.HttpServletRequestWrapper;  














import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;

import cn.net.vote.util.StringUtil;


public class ParameterRequestWrapper extends HttpServletRequestWrapper {  
      
    private Map<String , String[]> params = new HashMap<String, String[]>();  
    private final byte[] body;
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @SuppressWarnings("unchecked")  
    public ParameterRequestWrapper(HttpServletRequest request) {  
        // 将request交给父类，以便于调用对应方法的时候，将其输出，其实父亲类的实现方式和第一种new的方式类似  
        super(request);  
       // System.out.println("body:"+request.getParameter("body"));
        //将参数表，赋予给当前的Map以便于持有request中的参数  
        this.params.putAll(request.getParameterMap());
		 String bodyString = HttpHelper.getBodyString(request);
		 logger.info("请求body参数："+bodyString);
        body = bodyString.getBytes(Charset.forName("UTF-8"));
        this.modifyParameterValues();  
    }  
    //重载一个构造方法  
    public ParameterRequestWrapper(HttpServletRequest request , Map<String , Object> extendParams) {  
        this(request);  
        addAllParameters(extendParams);//这里将扩展参数写入参数表  
    }  
      
    public void modifyParameterValues(){//将parameter的值去除空格后重写回去  
    	
        Set<String> set =params.keySet();  
        Iterator<String> it=set.iterator();  
        while(it.hasNext()){  
           String key= (String) it.next();  
           String[] values = params.get(key);  
           if(key!=null&&!key.toLowerCase().contains("time")){
        	 values[0] = StringUtils.replace(StringUtil.replaceBlank(values[0]), "%", "\\%");
           }
           
           params.put(key, values);  
         }  
    }  
      
    @Override  
    public String getParameter(String name) {//重写getParameter，代表参数从当前类中的map获取  
        String[]values = params.get(name);  
        if(values == null || values.length == 0) {  
            return null;  
        }  
        return values[0];  
    }  
      
    public String[] getParameterValues(String name) {//同上  
         return params.get(name);  
    }  

   public void addAllParameters(Map<String , Object>otherParams) {//增加多个参数  
        for(Map.Entry<String , Object>entry : otherParams.entrySet()) {  
            addParameter(entry.getKey() , entry.getValue());  
        }  
    }  


    public void addParameter(String name , Object value) {//增加参数  
        if(value != null) {  
            if(value instanceof String[]) {  
                params.put(name , (String[])value);  
            }else if(value instanceof String) {  
                params.put(name , new String[] {(String)value});  
            }else {  
                params.put(name , new String[] {String.valueOf(value)});  
            }  
        }  
    }  
    
    @Override
    public BufferedReader getReader() throws IOException {
        return new BufferedReader(new InputStreamReader(getInputStream()));
    }

    @Override
    public ServletInputStream getInputStream() throws IOException {

        final ByteArrayInputStream bais = new ByteArrayInputStream(body);

        return new ServletInputStream() {
            @Override
            public int read() throws IOException {
                return bais.read();
            }

			@Override
			public boolean isFinished() {
				// TODO Auto-generated method stub
				return false;
			}

			@Override
			public boolean isReady() {
				// TODO Auto-generated method stub
				return false;
			}

			@Override
			public void setReadListener(ReadListener listener) {
				// TODO Auto-generated method stub
				
			}
        };
    }

    @Override
    public String getHeader(String name) {
        return super.getHeader(name);
    }

    @Override
    public Enumeration<String> getHeaderNames() {
        return super.getHeaderNames();
    }

    @Override
    public Enumeration<String> getHeaders(String name) {
        return super.getHeaders(name);
    }
    
}  